{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 作业二： \n",
    "### 阅读下面的代码 \n",
    "### np.random.seed(1) \n",
    "### X = np.random.randint(1, 10, size=30) \n",
    "### 此时X中的数据如下: \n",
    "### array([6, 9, 6, 1, 1, 2, 8, 7, 3, 5, 6, 3, 5, 3, 5, 8, 8, 2, 8, 1, 7, 8,7, 2, 1, 2 , 9, 9, 4, 9]) \n",
    "### 请将X处理为一个3列的矩阵，如下: 5分 \n",
    "### 将第三列中，小于等于3的修改为0、大于3且小于等于6的修改为1、大于6的修改为2，结果如下: 10分 \n",
    "### 假设这是10条样本数据，前两列是样本的两个特征，第3列是样本的分类标记，请分离出样本的特征和分类 标记，分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份) 5分 \n",
    "### 请用numpy的比较运算，通过 y_train 中的数据，分离出 X_train 中的3个分类，如下图 5分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.random.seed(1) \n",
    "X = np.random.randint(1, 10, size=30) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 6],\n",
       "       [1, 1, 2],\n",
       "       [8, 7, 3],\n",
       "       [5, 6, 3],\n",
       "       [5, 3, 5],\n",
       "       [8, 8, 2],\n",
       "       [8, 1, 7],\n",
       "       [8, 7, 2],\n",
       "       [1, 2, 9],\n",
       "       [9, 4, 9]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 请将X处理为一个3列的矩阵X\n",
    "x=X.reshape(-1,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 0, 0, 0, 1, 0, 2, 0, 2, 2]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出第三列,按规则修改\n",
    "# t = x[...,-1]\n",
    "t = x.T[2]\n",
    "\n",
    "def sorted_diff(x):\n",
    "    if x<=3:\n",
    "        return 0\n",
    "    elif x>3 and x<=6:\n",
    "        return 1\n",
    "    else:\n",
    "        return 2 \n",
    "t_temp = []\n",
    "for i in range(len(t)):\n",
    "    t_temp.append(sorted_diff(t[i]))\n",
    "t_temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 1],\n",
       "       [1, 1, 0],\n",
       "       [8, 7, 0],\n",
       "       [5, 6, 0],\n",
       "       [5, 3, 1],\n",
       "       [8, 8, 0],\n",
       "       [8, 1, 2],\n",
       "       [8, 7, 0],\n",
       "       [1, 2, 2],\n",
       "       [9, 4, 2]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将第三列中，小于等于3的修改为0、大于3且小于等于6的修改为1、大于6的修改为2，结果如下:\n",
    "\n",
    "b = x[:,2]\n",
    "for i in range(len(b)):\n",
    "    b[i] = sorted_diff(b[i])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设这是10条样本数据，前两列是样本的两个特征，第3列是样本的分类标记，请分离出样本的特征和分类 标记，\n",
    "# 分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请用numpy的比较运算，通过 y_train 中的数据，分离出 X_train 中的3个分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
